﻿package org.xhs.report.doc.ui
{
    import __AS3__.vec.*;
    import baidu.ui.events.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.doc.*;
    import baidu.wenku.doc.model.*;
    import flash.display.*;
    import flash.events.*;

    public class SingleViewer extends DocViewerBase implements IDocViewer
    {
        protected var containerHeight:Number;
        protected var pagesAtStage:Vector.<int>;
        protected var pagesPrevStage:Vector.<int>;

        public function SingleViewer()
        {
            init();
            return;
        }// end function

        override protected function initViewer() : void
        {
            super.initViewer();
            this.pagesAtStage = new Vector.<int>;
            return;
        }// end function

        override protected function setPagesPos(event:LocatEvent = null) : void
        {
            var _loc_2:int = 0;
            var _loc_3:BlankPage = null;
            _loc_2 = 0;
            var _loc_4:* = _pagesArray.length;
            docLocatModel.pagesYCoordinate = new Vector.<Number>;
            _loc_2 = 0;
            while (_loc_2 < _loc_4)
            {
                
                _loc_3 = _pagesArray[_loc_2] as BlankPage;
                _loc_3.scaleX = 1;
                _loc_3.visible = true;
                container.addChild(_loc_3);
                _loc_3.x = 0;
                _loc_3.y = _loc_2 ? (docLocatModel.pagesYCoordinate[(_loc_2 - 1)] + _pagesArray[(_loc_2 - 1)].height + DocConfig.PAGE_Y_PADDING) : (DocConfig.PAGE_Y_PADDING / 4);
                docLocatModel.pagesYCoordinate[_loc_2] = _loc_3.y;
                _loc_2++;
            }
            container.graphics.clear();
            container.graphics.beginFill(16711680, 0);
            container.graphics.drawRect(0, 0, _loc_3.width, _loc_3.y + _loc_3.height + DocConfig.PAGE_Y_PADDING / 4);
            container.graphics.endFill();
            container.addChild(_pagesArray[_pageth]);
            _pagesArray[docLocatModel.pageth].visible = true;
            this.setOptimalZoom();
            this.setPagesVisible();
            return;
        }// end function

        override protected function scrollPosChange(event:ScrollEvent) : void
        {
            dispatchEvent(new LocatEvent(LocatEvent.SCROLL_POS_CHANGE));
            return;
        }// end function

        protected function setPagesVisible() : void
        {
            if (!_pagesArray || !docLocatModel.pagesYCoordinate)
            {
                return;
            }
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            _loc_1 = 0;
            _loc_2 = this.pagesAtStage.length;
            this.pagesPrevStage = new Vector.<int>;
            while (++_loc_1 <= _loc_2)
            {
                
                this.pagesPrevStage.push(this.pagesAtStage[(_loc_1 - 1)]);
            }
            ++_loc_1 = 0;
            this.pagesAtStage = new Vector.<int>;
            _loc_2 = Math.min(_pagesArray.length, docLocatModel.pagesYCoordinate.length);
            var _loc_3:* = scrollPane.vScrollPosition / container.scaleX;
            var _loc_4:* = (scrollPane.vScrollPosition + docLocatModel.oriPageHeight) / container.scaleX;
            while (++_loc_1 < _loc_2)
            {
                
                if (docLocatModel.pagesYCoordinate[++_loc_1 - 1] + Config.pageHeight > _loc_3)
                {
                    this.pagesAtStage.push((_loc_1 - 1));
                    while (++_loc_1 <= _loc_2 && docLocatModel.pagesYCoordinate[++_loc_1 - 1] < _loc_4)
                    {
                        
                        this.pagesAtStage.push((_loc_1 - 1));
                    }
                    break;
                }
            }
            ++_loc_1 = 0;
            _loc_2 = this.pagesPrevStage.length;
            while (++_loc_1 <= _loc_2)
            {
                
                if (this.pagesAtStage.indexOf(this.pagesPrevStage[++_loc_1 - 1]) < 0)
                {
                    (_pagesArray[this.pagesPrevStage[(_loc_1 - 1)]] as BlankPage).hideLoading();
                }
            }
            ++_loc_1 = 0;
            _loc_2 = this.pagesAtStage.length;
            while (++_loc_1 <= _loc_2)
            {
                
                if (this.pagesPrevStage.indexOf(this.pagesAtStage[++_loc_1 - 1]) < 0)
                {
                    (_pagesArray[this.pagesAtStage[(_loc_1 - 1)]] as BlankPage).showLoading();
                }
            }
            if (storeModel.isStore && storePage)
            {
                storePage.hideLoading();
            }
            return;
        }// end function

        override protected function showPage(event:Event = null) : void
        {
            _pageth = docLocatModel.pageth;
            var _loc_2:* = _pagesArray[_pageth] as Sprite;
            scrollPane.vScrollPosition = _loc_2.y * (_zoomNumber ? (_zoomNumber) : (1));
            return;
        }// end function

        override public function setOptimalZoom() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            _loc_1 = (screenModel.stageWidth - DocConfig.DV_WIDTH_LEFT_PADDING - Config.DV_WIDTH_RIGHT_SCROLLBAR - DocConfig.DV_WIDTH_RIGHT_PADDING) / Config.pageWidth;
            _loc_2 = (screenModel.stageHeight - Config.TB_HEIGHT - Config.SB_HEIGHT) / (Config.pageHeight + DocConfig.PAGE_Y_PADDING);
            zoomModel.zoomValues[0] = Math.min(_loc_2, _loc_1);
            zoomModel.zoomValues[1] = Math.max(_loc_2, _loc_1);
            var _loc_4:int = 2;
            while (_loc_4 < zoomModel.zoomLevelNum)
            {
                
                zoomModel.zoomValues[_loc_4] = zoomModel.zoomValues[1] * Number(DocZoomModel.ZOOM_LEVELS[_loc_4]);
                _loc_4++;
            }
            _loc_3 = _loc_1;
            zoomModel.zoomLevel = _loc_1 > _loc_2 ? (1) : (0);
            resetContainerPosition();
            return;
        }// end function

        override public function set zoomNumber(param1:Number) : void
        {
            if (!scrollPane || !container)
            {
                return;
            }
            _zoomNumber = param1;
            container.scaleX = _zoomNumber;
            container.scaleY = _zoomNumber;
            scrollPane.update();
            container.x = Math.max((screenModel.stageWidth - container.width - Config.DV_WIDTH_RIGHT_SCROLLBAR) / 2, 0);
            container.y = 0;
            scrollPane.update();
            scrollPane.hScrollPosition = scrollPane.maxHScrollPosition / 2;
            dispatchEvent(new LocatEvent(LocatEvent.ZOOM_CHANGED));
            var _loc_2:* = _pagesArray[docLocatModel.pageth] as BlankPage;
            scrollPane.vScrollPosition = _loc_2.y * _zoomNumber;
            return;
        }// end function

    }
}
